package cn.tedu.boot07.controller;

import cn.tedu.boot07.entity.User;
import cn.tedu.boot07.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserController {
    @Autowired(required = false)
    UserMapper mapper;
    @RequestMapping("/reg")
    //如果客户端发出的是post异步请求并且提交的是参数JS(java script)对象,则必须使用@RequestBody
    public int reg(@RequestBody User user){
        System.out.println("user = " + user);
        User u=mapper.selectByUsername(user.getUsername());
        if (u!=null){
            return 2;
        }
        mapper.insert(user);
        return 1;
    }
    @RequestMapping("/login")
    public int login(@RequestBody User user){
        System.out.println("user = " + user);
        //通过输入的用户名查询数据库中的密码
        User u=mapper.selectByUsername(user.getUsername());
        if (u!=null){
            if (u.getPassword().equals(user.getPassword())){
                return 1;//登录成功
            }
            return 3;//密码错误
        }
        return 2;//用户名不存在
    }
}
